var pid = "";

function myPrint() {
		//病案主键
		var basicId = $("input[name='banum_checkbox']:checked").val();
		var printType = $("[name='combination_checkbox'][checked]").val();
		var printCopy =$("#input_fs").val();
		var basepath=$("#basepath").val();
		
        var	speed=1;
        //意思是选择被选中的checkbox
        //打开串口及签章机
        //var flag= OpenCom("2");
        
        //if(flag==0){
          //  RunStamp(2);
        //}
        
            $.ajax({
                cache: false,
                type:"get",
                url:basepath + "/print/getPrintPDFBase64ByMrBasicIdAndPrintType",
                dataType:"json",
                data:{
                	mrBasicId:basicId,
                	printTypeCode:printType
                },
                async: true,
                beforeSend: function(xhr){
                	
                },
                success:function(data) {
                	if (data.success) {
                		var pdfBase64 = data.data;
                		
                		 $(".alert_warn_").remove();
                         $("#myModal").append("<div class='alert_warn_new modal' id='dy_alert'><div class='warn_div_dq'><font class='font12' id='warn'>请稍后，正在调取病案...</font></div></div>");

                         $("#dy_alert").append("<div class='alert fade in bar_warn warn_dq' id='bar_close'><div  class='progress progress-striped active'><div class='bar' id='bar_dy' " +
                         		"style='width: 1%;'></div></div></div>");
                         //settime_progress($("#dy_alert"),num*0.3+2);
                         progress_bar_dq(10, 2);
                         //进度条开启
                         //progress_bar(speed);
                         My_Print(pdfBase64, printCopy);
                         //getDyStateNum(num,fs,data,fs);
                	} else {
                		 toastr.warning('病案暂未扫描，请咨询人工窗口！!');
                		 $(".alert_warn_new").remove();
                	}

                   
                }, error:function(){
                	toastr.warning('病案暂未扫描，请咨询人工窗口！!');
                    $(".alert_warn_new").remove();
                }
            });
};


function My_Print(pdfBase64, printCopy){
    //var docPath=$("#docPath").val();
    var basepath=$("#basepath").val();

    var date=new Date();
    var month=date.getMonth()+1;
    var strDate=date.getDate();
    var year=date.getFullYear();
    var hour=date.getHours();
    var minute=date.getMinutes();
    var second=date.getSeconds();
    if(month>=1&&month<=9){
        month="0"+month;
    }
    if(strDate>=0&&strDate<=9){
        strDate="0"+strDate;
    }
    if(hour>=0&&hour<10){
        hour="0"+hour;
    }
    if(minute>=0&&minute<10){
        minute="0"+minute;
    }
    if(second>=0&&second<10){
        second="0"+second;
    }
    
    var currentday=year+"-"+month+"-"+strDate+" "+date.getHours()+":"+minute+":"+second+"     自助打印";

    LODOP=getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
    LODOP.PRINT_INIT("自助打印");
    
	LODOP.SET_LICENSES("北京胡桃计算机技术有限公司","C8F38CEFB142B5073FDC2522C736FF20B97","北京胡桃計算機技術有限公司","C8044CE74282C69E73C8FCE82DEF3217C52");
	LODOP.SET_LICENSES("THIRD LICENSE","","Beijing Walnut Computer Technology Co., Ltd","A8F95FB0BD86C3B84BD0FB586910B8C4D5E");
	
	LODOP.SET_PRINT_COPIES(printCopy)
	LODOP.ADD_PRINT_PDF(0,0,"100%","100%", pdfBase64);
	p_id = LODOP.PRINT()
}

function getDyStateNum(num,fs,data,current){
    var basepath=$("#basepath").val();
    printNum=GetStampCount();
    
    if(printCurrent>255){
        printTime=parseInt(printCurrent/255);
    }
    
    pstate= m607state(GetStatus());
    if(pstate!='true'){
        machineFailure(pstate);
        if(pstate=='卡纸'){
            DelPrinterJob();
            alert("打印机卡纸，未扣费。请到其他自助机再次打印！");
        }else{
            alert("请联系窗口工作人员，打印机"+pstate);
        }
    }
    
    //获得打印任务号
    if(p_id!=''&&p_id!= undefined){

        var flag=true;

        var state=getStatusValue('PRINT_STATUS_EXIST',p_id,'');
        var status=getStatusValue('PRINT_STATUS_ID',p_id,'');
        //var sta=getStatusValue('PRINT_STATUS_TEXT',p_id,'');
        //alert(state+";打印机状态："+status);
        if(printCurrent>(printTime*255+printNum)){
            if(status==8272){
                alert("打印机缺纸，请联系工作人员!");
            }
            if(status==8210){
                alert("打印机故障，请联系工作人员!");
            }
            flag= true;
        }else{
            flag= false;
        }
        //检测到未打印完，延时检测
        if(flag){

            $.ajax({
                cache: true,
                type:"POST",
                url:basepath+"/spmr/getDelay",
                dataType:"json",
                data:{delay:2000},
                async: true,
                beforeSend: function(xhr){
                    xhr.setRequestHeader(header, token);
                },
                success:function(data1){
                    getDyStateNum(num,fs,data,current);
                }
            });
        }else{
            //一次打印完成，再打印下一次
            current=current-1;
            if(current!=0){
                if($("#choiceWarn").length > 0){
                    $("#choiceWarn").remove();
                }
                $("#myModal").append("<div class='alert fade in ' id='choiceWarn'><div class='warn_div'><font class='font9' id='warn'>第"+(fs-current)+"份病案，打印完成请取出装订！</font></div></div>");

                //$("#bar_font").html("正在打印第"+(fs-current+1)+"份，请稍后！");
                My_Print(data);
                getDyStateNum(num,fs,data,current);
            }else{
                //扣费
                $.ajax({
                    cache: true,
                    type:"POST",
                    url:basepath+"/spmr/doPayMdeilcalRecord",
                    dataType:"json",
                    data:{
                        onlyId			:$("#onlyId").val(),
                        cardno			:$("#outpatient").val(),
                        name				:$("#name").html(),
                        patientIdCardNo	:$("#idcard").html(),
                        num				:num*fs,
                        paymentVoucher	:$("#paymentVoucher").val()
                    },
                    async: true,
                    beforeSend: function(xhr){
                        xhr.setRequestHeader(header, token);
                    },
                    success:function(data){

                    }
                });
                //单次出院打印完成，关闭窗口
                $.ajax({
                    cache: true,
                    type:"POST",
                    url:basepath+"/spmr/getDelay",
                    dataType:"json",
                    data:{delay:100*num*fs},
                    async: true,
                    beforeSend: function(xhr){
                        xhr.setRequestHeader(header, token);
                    },
                    success:function(data){
                        closeWindow();
                    }
                });
            }

        }

    }else{
        //setTimeout(closeWindow(),8000+1000*num*fs);
    }
}






	